package com.gh.shopmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gh.common.utils.PageUtils;
import com.gh.common.utils.Query;
import com.gh.shopmall.product.dao.AttrGroupDao;
import com.gh.shopmall.product.entity.AttrEntity;
import com.gh.shopmall.product.entity.AttrGroupEntity;
import com.gh.shopmall.product.service.AttrGroupService;
import com.gh.shopmall.product.service.AttrService;
import com.gh.shopmall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<>());
            return new PageUtils(page);
        } else {
            String key = (String) params.get("key");
            LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().lambda()
                    .eq(AttrGroupEntity::getCatelogId, catelogId);
            if (!StringUtils.isEmpty(key)) {
                queryWrapper.and(obj -> {
                    obj.eq(AttrGroupEntity::getAttrGroupId, key)
                            .or().like(AttrGroupEntity::getAttrGroupName, key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类id查询出所有分组，以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.查询分组信息
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>()
                .lambda().eq(AttrGroupEntity::getCatelogId, catelogId)
        );
        if(!CollectionUtils.isEmpty(attrGroupEntities)){
            List<AttrGroupWithAttrsVo> list = attrGroupEntities.stream().map(attrGroupEntity -> {
                AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
                BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
                List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
                attrGroupWithAttrsVo.setAttrs(attrs);
                return attrGroupWithAttrsVo;
            }).collect(Collectors.toList());
            return list;
        }
        return null;
    }

}